//
//  UserInfoView.m
//  CloudWeibo
//
//  Created by CLOUD on 1/15/15.
//  Copyright (c) 2015 Cloud. All rights reserved.
//

#import "UserInfoView.h"
#import "UserModel.h"
#import "UIImageView+WebCache.h"
#import "RectButton.h"

@implementation UserInfoView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"UserInfoView" owner:self options:nil]lastObject];
        view.backgroundColor = Color(245, 245, 245, 1);
        [self addSubview:view];
        self.size = view.size;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    NSString *imageUrl = self.user.avatar_large;
    [self.userImage setImageWithURL:[NSURL URLWithString:imageUrl]];
    
    self.nameLabel.text = self.user.screen_name;
    
    NSString *gender = self.user.gender;
    
    NSString *sex = @"未知";
    
    if ([gender isEqualToString:@"f"]) {
        sex = @"男";
    }else if ([gender isEqualToString:@"m"]) {
        sex = @"女";
    }
    
    NSString *location = self.user.location;
    if (location == nil) {
        location = @"";
    }
    
    self.addressLabel.text = [NSString stringWithFormat:@"%@  %@", sex, location];
    
    self.infoLabel.text = self.user.description == nil ? @"" : self.user.description;
    
    NSString *count = [self.user.statuses_count stringValue];
    self.countLabel.text = [NSString stringWithFormat:@"共%@条微博", count];
    
    long followersCount = [self.user.followers_count longValue];
    NSString *fans = [NSString stringWithFormat:@"%ld",followersCount];
    if (followersCount >= 10000) {
        fans = [NSString stringWithFormat:@"%ld万", followersCount/10000];
    }
    
    self.fansButton.title = @"粉丝";
    self.fansButton.subTitle = fans;
    
    self.followButton.title = @"关注";
    self.followButton.subTitle = [self.user.friends_count stringValue];
    
}

- (void)dealloc {
    [_userImage release];
    [_nameLabel release];
    [_addressLabel release];
    [_infoLabel release];
    [_countLabel release];
    [_followButton release];
    [_fansButton release];
    [super dealloc];
}
@end
